import config from '../config/config.js'
import documents from './document.js'

const GRAPHQL_URL = 'https://api.github.com/graphql'
const GITHUB_API = 'https://api.github.com/repos'

const { username, repository, token } = config
const blog = `${GITHUB_API}/${username}/${repository}`
const accessToken = token.join('')
const open = `state=open&access_token=${accessToken}`
const closed = `state=closed&access_token=${accessToken}`

// 构建 GraphQL
const createCall = async (document) => {
    const payload = JSON.stringify({ query: document })
	return new Promise(resolve => {
		uni.request({
			url:GRAPHQL_URL,
			method:'POST',
			header:{
				 Authorization: `token ${accessToken}`
			},
			data: payload,
			success(res) {
			 if(res.statusCode == 200){
				 resolve(res.data.data)
			 }
			}
		})
	})
}

// 获取文章数量
export const queryArchivesCount = async () => {
	let res = await createCall(documents.queryArchivesCount({ username, repository }))
	return res.repository.issues.totalCount
}

// 获取灵感数量
export const queryInspirationCount = async () => {
	let res = await createCall(documents.queryInspirationCount({ username, repository }))
	return res.repository.issues.totalCount
}


// 按分类 & 标签筛选文章数量
export const queryFilterArchivesCount = async (label='', milestone='' ) =>{
	let res = await createCall(documents.queryFilterArchivesCount({ username, repository, label, milestone }))
	return res.search.issueCount
}

// 获取文章列表
export const queryPosts = async (page = 1,pageSize = 10,filter = '') => {
    const url = `${blog}/issues?${open}&page=${page}&per_page=${pageSize}${filter}`
	return new Promise((resolve,reject) => {
		try {
			uni.request({
				url: url,
				method: 'GET',
				success(res) {
					resolve(res.data)
				}
			})
		} catch (err) {
		  reject(err)
		}
	})
}

// 获取单篇文章
export const queryPost = async (number) => {
	return new Promise((resolve,reject) => {
		try {
		  const url = `${blog}/issues/${number}?${open}`
		  uni.request({
		  	url: url,
		  	method: 'GET',
		  	success(res) {
		  		resolve(res.data)
		  	}
		  })
		} catch (err) {
		    reject(err)
		}
	})
}

//获取所有分类
export const queryAllClasses = async () => {
	return new Promise((resolve,reject) => {
		try {
		  const url = `${blog}/milestones?access_token=${accessToken}`
		  uni.request({
		  	url: url,
		  	method: 'GET',
		  	success(res) {
		  		resolve(res.data)
		  	}
		  })
		} catch (err) {
		    reject(err)
		}
	})
}

//获取所有标签
export const queryAllLabel = async () => {
	return new Promise((resolve,reject) => {
		try {
		  const url = `${blog}/labels?access_token=${accessToken}`
		  uni.request({
		  	url: url,
		  	method: 'GET',
		  	success(res) {
		  		resolve(res.data)
		  	}
		  })
		} catch (err) {
		    reject(err)
		}
	})
}
export const queryInspirations = async(page = 1,pageSize = 10) => {
	const url = `${blog}/issues?${closed}&page=${page}&per_page=${pageSize}&milestone=3`
	return new Promise((resolve,reject) => {
		try {
			uni.request({
				url: url,
				method: 'GET',
				success(res) {
					resolve(res.data)
				}
			})
		} catch (err) {
		  reject(err)
		}
	})
}